import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

import LoginVue from '@/components/Login.vue';
import Register from "@/components/Register"
import Home from "@/views/di3bu4lan2/Home"
import QiangGuotong from "@/views/di3bu4lan2/QiangGuotong"
import BaiLing from "@/views/di3bu4lan2/BaiLing"
import DianShitai from "@/views/di3bu4lan2/DianShitai"
import DianTai from "@/views/di3bu4lan2/DianTai"
import MyVue from "@/views/My"
import Chat from '@/views/Chat'
import ContentDetails from "@/views/di3bu4lan2/DianTai/ContentDetails"
import DianShitaineirong from "@/views/di3bu4lan2/DianShiTai/DianShitaineirong"
import HomeContent from "@/views/di3bu4lan2/Home/HomeContent"
const routes = [
  {
    path: '/',
    redirect: '/home',
    meta: { title: '首页' }
  },
  {
    path: '/home',
    name: 'home',
    meta: { title: '首页' },
    component: Home
  },
  {
    path: '/qiangguotong',
    name: 'qiangguotong',
    meta: { title: '强国通' },
    component: QiangGuotong
  },
  {
    path: '/bailing',
    name: 'bailing',
    meta: { title: '百灵' },
    component: BaiLing
  },
  {
    path: '/dianshitai',
    name: 'dianshitai',
    meta: { title: '电视台' },
    component: DianShitai
  },
  {
    path: '/diantai',
    name: 'diantai',
    meta: { title: '电台' },
    component: DianTai
  },
  {
    path: '/register',
    name: 'register',
    component: Register
  },
  {
    path: '/log',
    name: 'log',
    component: LoginVue
  },
  {
    path: '/my',
    name: 'my',
    component: MyVue
  },
  {
    path: '/chat',
    name: 'chat',
    component: Chat,
    props: true
  },
  {
    path: '/contentdetails',
    name: 'contentdetails',
    component: ContentDetails,
    props: true,
  },
  {
    path: '/dianshitaineirong',
    name: 'dianshitaineirong',
    component: DianShitaineirong,
    props: true,
  },
  {
    path: '/homecontent',
    name: 'homecontent',
    component: HomeContent,
    props: true,
  },
]

const router = new VueRouter({
  routes
})


// 路由守卫
router.beforeEach((to, from, next) => {

  let needTokenPage = ['log', 'register']
  //如果是去needTokenPage里的路由，直接去   否则都要判断有没有登录过
  if (needTokenPage.includes(to.name)) return next();

  // 如果没有登录过的信息，那就跳转到登录页面
  if (!localStorage.getItem('userxx')) {
    router.push('/log').catch(e => { })
  } else {
    next();
  }
})
export default router
